package com.limf.demo.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自定义的线程池构造工厂
 *
 */
public class CustomThreadFactory implements ThreadFactory {

	private static final AtomicInteger poolNumber = new AtomicInteger(1);
	private final ThreadGroup group;
	private final AtomicInteger threadNumber = new AtomicInteger(1);
	private final String namePrefix;
	
	public CustomThreadFactory() {
		SecurityManager s = System.getSecurityManager();
		group = (s!=null)?s.getThreadGroup():Thread.currentThread().getThreadGroup();
		namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-";
		
	}
	
	@Override
	public Thread newThread(Runnable r) {

		String name = namePrefix + threadNumber.getAndIncrement();
		Thread t = new Thread(group, r, name, 0);
		System.out.println("线程池创建，线程名称为："+name);
		if(t.isDaemon()) 
			t.setDaemon(false);
		if(t.getPriority() != Thread.NORM_PRIORITY)
			t.setPriority(Thread.NORM_PRIORITY);
		
		return t;
	}

}
